package sz.com.postoffice.jxyzwgcase.service.impl;

import com.github.pagehelper.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import sz.com.postoffice.jxyzwgcase.core.TableDataInfo;
import sz.com.postoffice.jxyzwgcase.dao.BaseDao;
import sz.com.postoffice.jxyzwgcase.dao.master.DailyIncomeSummaryDao;
import sz.com.postoffice.jxyzwgcase.pojo.*;
import sz.com.postoffice.jxyzwgcase.service.DailyIncomeSummaryService;
import sz.com.postoffice.jxyzwgcase.service.MonthPlanService;
import sz.com.postoffice.jxyzwgcase.service.UpdateDailyIncomeService;
import sz.com.postoffice.jxyzwgcase.service.UserAdoService;
import sz.com.postoffice.jxyzwgcase.utils.DateStrUtil;

import javax.annotation.Resource;
import java.util.List;


/**
 * @ClassName DailyIncomeSummaryServiceImpl
 * @Deacription 每日收入汇总
 * @Author jmx
 * @Date 2020/11/12 18:00
 * @Version 1.0
 **/
@Service
@Slf4j
public class UpdateDailyIncomeServiceImpl implements UpdateDailyIncomeService {
    @Autowired
    private MonthPlanService monthPlanService;
    @Autowired
    private DailyIncomeSummaryService dailyIncomeSummaryService;
    @Autowired
    private UserAdoService userAdoService;

    //根据日期查出当天的数据
    //遍历每条数据，根据usercode查询用户,补userName,deptCode,dailyVisit,monthlyvisit,monthplan

    @Override
    public void updateDailyIncomeByDate(String statDate) {
        DailyIncomeSummary queryParam = new DailyIncomeSummary();
        queryParam.setStatDate(statDate);
        List<DailyIncomeSummary> dailyIncomeSummaries = dailyIncomeSummaryService.queryAll(queryParam);
        if (! CollectionUtils.isEmpty(dailyIncomeSummaries)) {
            for (DailyIncomeSummary dailyIncomeSummary : dailyIncomeSummaries) {
                UserAdo queryAdo = new UserAdo();
                queryAdo.setUserCode(dailyIncomeSummary.getUserCode());
                UserAdo userAdo = userAdoService.findByEntity(queryAdo);
                if (userAdo != null) {
                    dailyIncomeSummary.setUserName(userAdo.getUserName());
                    dailyIncomeSummary.setDeptCode(userAdo.getDeptCode());
                }
                dailyIncomeSummary.setMonthPlanedIncome(getMonthPlan(dailyIncomeSummary.getUserCode(), statDate));
                dailyIncomeSummary.setDailyVisit(dailyIncomeSummaryService.getDailyVisitByUser(dailyIncomeSummary.getUserCode(), statDate));
                String firstDay = DateStrUtil.getFirstDayOfMonth(statDate);
                dailyIncomeSummary.setMonthlyVisit(dailyIncomeSummaryService.sumVisit(dailyIncomeSummary.getUserCode(), firstDay, statDate));
                dailyIncomeSummaryService.update(dailyIncomeSummary);
            }
        }
    }

    private Double getMonthPlan(String userCode, String statDate) {
        MonthPlan tMonthPlan = new MonthPlan();
        tMonthPlan.setUserCode(userCode);
        String strDate = statDate.trim();
        tMonthPlan.setPlanMonth(strDate.substring(0, strDate.lastIndexOf("-")));
        MonthPlan monthPlan = monthPlanService.findByEntity(tMonthPlan);
        if (monthPlan != null) {
            return monthPlan.getPlanAmount();
        } else {
            return 0.0;
        }
    }
}
